home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / ifcico / nodebld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-09  |  4.8 KB  |  242 lines

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #ifdef HAS_NDBM_H
  9. #include <fcntl.h>
  10. #include <ndbm.h>
  11. #else
  12. #include <dbm.h>
  13. #endif
  14. #include <dirent.h>
  15.  
  16. #include "xutil.h"
  17. #include "lutil.h"
  18. #include "ftn.h"
  19. #include "config.h"
  20. #include "nodelist.h"
  21. #include "nlindex.h"
  22.  
  23. int nodebld(void)
  24. {
  25.     int typ,num,i,j,rc=0;
  26.     int lineno,entries,total;
  27.     fa_list *tmp;
  28.     FILE *ifp;
  29.     off_t off;
  30.     struct _loc *loc,*oldloc;
  31.     unsigned short hub;
  32.     unsigned char pflag;
  33.     char buf[256],*p,*q;
  34.     struct _ixentry addr;
  35.     faddr *tmpa;
  36.     datum key;
  37.     datum dat;
  38.     char *nm;
  39.  
  40.     loginf("Nodelist index rebuild requested");
  41.  
  42.     key.dptr=(char*)&addr;
  43.     key.dsize=sizeof(struct _ixentry);
  44.  
  45. #ifdef HAS_NDBM_H
  46.     if (nldb != NULL)
  47.     {
  48.         dbm_close(nldb);
  49.         nldb=NULL;
  50.     }
  51. #else
  52.     dbmclose();
  53. #endif
  54.  
  55.     nm=xstrcpy(nlbase);
  56.     nm=xstrcat(nm,INDEX);
  57.     nm=xstrcat(nm,".dir");
  58.  
  59.     if ((ifp=fopen(nm,"w"))) fclose(ifp);
  60.     else
  61.     {
  62.         logerr("$cannot create new %s",nm);
  63.         return 1;
  64.     }
  65.     strcpy(nm+strlen(nm)-3,"pag");
  66.     if ((ifp=fopen(nm,"w"))) fclose(ifp);
  67.     else
  68.     {
  69.         logerr("$cannot create new %s",nm);
  70.         return 1;
  71.     }
  72.     nm[strlen(nm)-4]='\0';
  73. #ifdef HAS_NDBM_H
  74.     if ((nldb=dbm_open(nm,O_RDWR,0600)) == NULL) rc=1;
  75. #else
  76.     rc=dbminit(nm);
  77. #endif
  78.     if (rc)
  79.     {
  80.         logerr("$cannot open dbm \"%s\"",nm);
  81.         free(nm);
  82.         return rc;
  83.     }
  84.     free(nm);
  85.  
  86.     rc=0;
  87.     total=0;
  88.     for (i=0,tmp=nllist;tmp;tmp=tmp->next,i++)
  89.     if (nodevector[i].fp)
  90.     {
  91.         debug(20,"making index for \"%s[.???]\"",tmp->addr->name);
  92.         addr.zone=0;
  93.         addr.net=0;
  94.         addr.node=0;
  95.         addr.point=0;
  96.         hub=0;
  97.         if (tmp->addr)
  98.         {
  99.             addr.zone=tmp->addr->zone;
  100.             addr.net=tmp->addr->net;
  101.             addr.node=tmp->addr->node;
  102.             addr.point=tmp->addr->point;
  103.         }
  104.         entries=0;
  105.         lineno=0;
  106.         while (!feof(nodevector[i].fp))
  107.         {
  108.             off=ftell(nodevector[i].fp);
  109.             lineno++;
  110.             if (fgets(buf,sizeof(buf)-1,nodevector[i].fp) == NULL)
  111.                 continue;
  112.             if (*(buf+strlen(buf)-1) != '\n')
  113.             {
  114.                 while (fgets(buf,sizeof(buf)-1,nodevector[i].fp) &&
  115.                        (*(buf+strlen(buf)-1) != '\n')) /*void*/;
  116.                 logerr("nodelist %d(%u): too long line junked",
  117.                     i,lineno);
  118.                 continue;
  119.             }
  120.             if (*(p=buf+strlen(buf)-1) == '\n') *p--='\0';
  121.             if (*p == '\r') *p='\0';
  122.             if ((buf[0] == ';') || (buf[0] == '\0')) continue;
  123.             if ((p=strchr(buf,','))) *p++='\0';
  124.             if ((q=strchr(p,','))) *q++='\0';
  125.             typ=NONE;
  126.             pflag=0;
  127.             if (buf[0] == '\0') typ=NODE;
  128.             else if (strcasecmp(buf,"Boss") == 0) 
  129.             {
  130.                 if ((tmpa=parsefnode(p)) == NULL)
  131.                 {
  132.                     logerr("%s(%u): unparsable Boss addr \"%s\"",
  133.                         tmp->addr->name,lineno,p);
  134.                     continue;
  135.                 }
  136.                 if (tmpa->zone) addr.zone=tmpa->zone;
  137.                 addr.net=tmpa->net;
  138.                 addr.node=tmpa->node;
  139.                 tidy_faddr(tmpa);
  140.                 typ=NONE;
  141.                 continue; /* no further processing */
  142.             }
  143.             else for (j=0;pkey[j].key;j++)
  144.                 if (strcasecmp(buf,pkey[j].key) == 0) 
  145.                 {
  146.                     typ=pkey[j].type;
  147.                     pflag=pkey[j].pflag;
  148.                 }
  149.             if (typ == NONE)
  150.             {
  151.                 for (q=buf;*q;q++) if (*q < ' ') *q='.';
  152.                 logerr("%s(%u): unidentified entry \"%s\"",
  153.                     tmp->addr->name,lineno,buf);
  154.                 continue;
  155.             }
  156.             debug(21,"got \"%s\" as \"%s\" typ %d",buf,p,typ);
  157.             if ((num=atoi(p)) == 0)
  158.             {
  159.                 logerr("%s(%u): bad numeric \"%s\"",
  160.                     tmp->addr->name,lineno,p);
  161.                 continue;
  162.             }
  163.  
  164.             /* first check it for being a hub */
  165.  
  166.             if (typ == NODE)
  167.             {
  168.                 if (pflag == HUB) hub=num;
  169.                 /* else it is under the same hub */
  170.             }
  171.             else hub=0; /* reset hub assignment */
  172.  
  173.             /* now update the current address */
  174.  
  175.             switch (typ)
  176.             {
  177.             case ZONE:    addr.zone=num;
  178.                     addr.net=num;
  179.                     addr.node=0;
  180.                     addr.point=0;
  181.                     break;
  182.             case NET:    addr.net=num;
  183.                     addr.node=0;
  184.                     addr.point=0;
  185.                     break;
  186.             case NODE:    addr.node=num;
  187.                     addr.point=0;
  188.                     break;
  189.             case POINT:    addr.point=num;
  190.                     break;
  191.             }
  192.             debug(21,"put: %u:%u/%u.%u as (%u,%lu)",
  193.                 addr.zone,addr.net,addr.node,
  194.                 addr.point,i,off);
  195. #ifdef HAS_NDBM_H
  196.             dat=dbm_fetch(nldb,key);
  197. #else
  198.             dat=fetch(key);
  199. #endif
  200.             oldloc=(struct _loc *)dat.dptr;
  201.             if (oldloc && (oldloc->nlnum != i))
  202.             {
  203.                 loc=(struct _loc *)xmalloc(sizeof(struct _loc)+
  204.                     dat.dsize);
  205.                 loc->nlnum=i;
  206.                 loc->off=off;
  207.                 loc->hub=hub;
  208.                 memcpy(loc+sizeof(struct _loc),oldloc,
  209.                     dat.dsize);
  210.                 dat.dsize+=sizeof(struct _loc);
  211.             }
  212.             else
  213.             {
  214.                 loc=(struct _loc *)xmalloc(sizeof(struct _loc));
  215.                 loc->nlnum=i;
  216.                 loc->off=off;
  217.                 loc->hub=hub;
  218.                 dat.dsize=sizeof(struct _loc);
  219.             }
  220.             dat.dptr=(char *)loc;
  221. #ifdef HAS_NDBM_H
  222.             if (dbm_store(nldb,key,dat,DBM_REPLACE))
  223. #else
  224.             if (store(key,dat))
  225. #endif
  226.             {
  227.                 logerr("cannot store %u:%u/%u.%u as (%u,%lu)",
  228.                     addr.zone,addr.net,addr.node,
  229.                     addr.point,i,off);
  230.             }
  231.             free(loc);
  232.             entries++;
  233.             total++;
  234.         }
  235.         loginf("%d entries in nodelist \"%s[.???]\"",
  236.             entries,tmp->addr->name);
  237.     }
  238.     loginf("Total %d entries in nodelist index",total);
  239.  
  240.     return rc;
  241. }
  242.